package org.csu.mypetstore.api.controller.front;

import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.Cart;
import org.csu.mypetstore.api.entity.Item;
import org.csu.mypetstore.api.entity.ItemInventory;
import org.csu.mypetstore.api.entity.Product;
import org.csu.mypetstore.api.persistence.ItemInventoryMapper;
import org.csu.mypetstore.api.persistence.ItemMapper;
import org.csu.mypetstore.api.persistence.ProductMapper;
import org.csu.mypetstore.api.service.CartService;
import org.csu.mypetstore.api.service.CatalogService;
import org.csu.mypetstore.api.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    Cart cart;
    @Autowired
    CatalogService catalogService;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ItemInventoryMapper itemInventoryMapper;

    @GetMapping("/{userid}")
    @ResponseBody
    public CommonResponse<CartVO> viewCart(@PathVariable("userid") String userid){
        return cartService.getCartItemByUserid(userid);
    }

    @GetMapping("/{userid}/{itemid}/add")
    @ResponseBody
    public CommonResponse<CartVO> addItem(@PathVariable("userid") String userid,@PathVariable("itemid") String itemid){
        cart.setUserid(userid);
        cart.setItemid(itemid);
        Item item = itemMapper.selectById(itemid);//此处在catalogservice中未提供相应接口，为了防止git提交冲突，在此先凑合一下
        cart.setProductid(item.getProductId());
        Product product = productMapper.selectById(item.getProductId());
        cart.setDescription(product.getDescription());
        ItemInventory itemInventory = itemInventoryMapper.selectById(itemid);
        if (itemInventory.getQuantity()<=0){
            cart.setInstock("0");
        }else {
            cart.setInstock("1");
        }
        cart.setQuantity(1);
        cart.setListprice(item.getListPrice());
        cart.setTotalprice(item.getListPrice());//此处插入商品个数为1listprice*1=listprice,简写为listprice
        return cartService.insertCartItem(cart);
    }

    @GetMapping("/{userid}/{itemid}/remove")
    @ResponseBody
    public CommonResponse<CartVO> removeItem(@PathVariable("userid") String userid,@PathVariable("itemid") String itemid){
        return cartService.deleteCartitemByUserid(userid,itemid);
    }

    @GetMapping("/{userid}/{itemid}/{quantity}/update")
    @ResponseBody
    public CommonResponse<BigDecimal> updateCart(@PathVariable("userid") String userid,@PathVariable("itemid") String itemid,@PathVariable("quantity") int quantity){
        return cartService.updateQuantity(userid,quantity,itemid);
    }

    @GetMapping("/{userid}/clear")
    @ResponseBody
    public CommonResponse<Cart> removeAll(@PathVariable("userid") String userid){
        return cartService.deleteByUserid(userid);
    }
}
